package com.sunshine.lnuplus.utils

import com.google.gson.Gson
import com.sunshine.lnuplus.model.InitData
import com.sunshine.lnuplus.model.XiaoAiBean
import com.sunshine.lnuplus.model.XiaoAiCourseBean
import java.lang.StringBuilder

/**
 * Created by sunshine on 2020/4/9
 */
object XiaoAiUtils {

    fun lessonToXiaoAiJson(deviceId: String, userId: String) : ArrayList<String> {
        val xiaoAiBeanList = ArrayList<String>()
        var xiaoAiCourseList: ArrayList<XiaoAiCourseBean> = ArrayList()
        var count = 1
        InitData.lessonList?.forEach {
            if(count++ % 11 == 0 || count > InitData.lessonList!!.size){
                val xiaoAiBean = XiaoAiBean(
                    0,
                    xiaoAiCourseList
                )
                xiaoAiBeanList.add(Gson().toJson(xiaoAiBean))
                xiaoAiCourseList = ArrayList()
            }else{
                val sections = StringBuilder()
                val style = StringBuilder()
                for(i in it.start..it.end){
                    sections.append(i).append(",")
                }
                val color = if(it.color == "0"){
                    var tempColor = String.format("%08x",ColorUtils.setBackgroundColor(it.lessonName))
                    "#" + tempColor.substring(2, tempColor.length)
                }else{
                    it.color
                }
                style.append("{\"color\":\"#FFFFFF\",\"background\":\"").append(color).append("\"}")
                val xiaoAiCourseBean = XiaoAiCourseBean(
                    it.lessonName,
                    it.teacher,
                    it.location,
                    it.dayOfWeek,
                    style.toString(),
                    deviceId,
                    userId.toInt(),
                    it.weeks.toString().substring(1, it.weeks.toString().length - 1).replace(" ", ""),
                    sections.toString().substring(0, sections.toString().length - 1),
                    ""
                )
                xiaoAiCourseList.add(xiaoAiCourseBean)
            }
        }
        return xiaoAiBeanList
    }
}